package com.schoolvisitors.vo;

import com.schoolvisitors.entity.*;
import com.schoolvisitors.enums.ReservationStatusEnum;
import com.schoolvisitors.enums.ReservationTypeEnum;
import com.schoolvisitors.exception.BusinessException;
import com.schoolvisitors.response.ResultCode;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Slf4j
@Data
public class ReservationVo {
    private Long id;
    private LocalDate reservationDate;
    private Long roomId;
    private LocalDate createDate;

    private String realName;
    private LocalTime startTime;
    private LocalTime endTime;

    private ReservationTypeEnum type;
    //是否未签退
    private boolean isNotSignedOut;

    private String error;
    private String roomTypeName;
    private String roomSubTypeName;
    private Long roomNumber;
    private int cubicleNumber;
    private String floorName;
    private Long floorId;
    private boolean clockIn;
    private LocalDateTime clockInTime;
    private LocalDateTime clockOutTime;
    private boolean clockOut;
    private ReservationStatusEnum status;
    private String statusName;
    private String instrumentName;
    private Long instrumentId;
    private Short floorNumber;
    private String roomSubType;
    private String instrumentImgName;

    public static ReservationVo toVo(Reservation reservation) {
        ReservationVo reservationVo = new ReservationVo();
        //log.info(reservation.toString());

        if (reservation.getType().name().startsWith("CUBICLE_")) {
            Room room = reservation.getRoom();
            reservationVo.setRoomSubTypeName(room.getSubType()); // 设置房间具体类型名称
            reservationVo.setRoomTypeName(room.getType().getDesc());
            reservationVo.setRoomNumber(room.getNumber()); // 设置房间号

            reservationVo.setRoomId(room.getId());
            reservationVo.setFloorName(room.getFloor().getName());
            reservationVo.setCubicleNumber(reservation.getCubicleNumber());
        } else {
            Room room = reservation.getInstrument().getRoom();
            reservationVo.setRoomSubTypeName(room.getSubType());
            reservationVo.setRoomTypeName(room.getType().getDesc());
            reservationVo.setRoomNumber(room.getNumber());
            Floor floor = room.getFloor();
            reservationVo.setFloorId(floor.getId());
            reservationVo.setFloorNumber(floor.getNumber());
            reservationVo.setFloorName(floor.getName());
            reservationVo.setInstrumentImgName(reservation.getInstrument().getImgName());
            reservationVo.setInstrumentName(reservation.getInstrument().getName());
            reservationVo.setInstrumentId(reservation.getInstrument().getId());
        }

        TimeSlot timeSlot = reservation.getTimeSlot();
        User user = reservation.getUser();

        Long reservationId = reservation.getId();

        boolean clockIn = reservation.getCheckInTime() != null;
        boolean clockOut = reservation.getCheckOutTime() != null;

        reservationVo.setClockIn(clockIn);
        reservationVo.setClockOut(clockOut);
//            log.info("{} {}", in, in == null);
//            log.info("{}" + cubicleReservation.getDate());
        reservationVo.setId(reservationId);
        reservationVo.setReservationDate(reservation.getDate());  // 设置预定日期

        reservationVo.setCreateDate(LocalDate.from(reservation.getCreateAt()));      // 设置创建日期
        reservationVo.setRealName(user.getRealName());  // 设置用户真实姓名
        reservationVo.setStartTime(timeSlot.getStartTime());  // 设置开始时间（14:00:00）
        reservationVo.setEndTime(timeSlot.getEndTime()); // 设置结束时间（16:00:00）

        reservationVo.setStatus(reservation.getStatus());
        reservationVo.setStatusName(reservation.getStatus().getDesc());
        reservationVo.setType(reservation.getType());
        reservationVo.setClockInTime(reservation.getCheckInTime());
        reservationVo.setClockOutTime(reservation.getCheckOutTime());
        // 获取预约日期和时间合成完整的预约开始时间
        LocalDateTime reservationStartDateTime = LocalDateTime.of(reservation.getDate(), timeSlot.getStartTime());
        LocalDateTime now = LocalDateTime.now();

        return reservationVo;
    }
}
